package top.lyunz.fangkeji.controller.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import top.lyunz.fangkeji.bean.AppointmentRecorSync;
import top.lyunz.fangkeji.bean.AppointmentRecord;
import top.lyunz.fangkeji.bean.AttendanceConfig;
import top.lyunz.fangkeji.bean.AttendanceRecord;
import top.lyunz.fangkeji.bean.Equipment;
import top.lyunz.fangkeji.bean.Fuser;
import top.lyunz.fangkeji.bean.TravelRecords;
import top.lyunz.fangkeji.bean.TravelRecordsadd;
import top.lyunz.fangkeji.bean.selAttendanceRecord;
import top.lyunz.fangkeji.bean.travelRecordsBean;
import top.lyunz.fangkeji.service.AppointmentRecordService;
import top.lyunz.fangkeji.service.AttendanceConfigService;
import top.lyunz.fangkeji.service.AttendanceRecordService;
import top.lyunz.fangkeji.service.TravelRecordsService;
import top.lyunz.fangkeji.service.appointmentMemberService;
import top.lyunz.fangkeji.service.equipmentService;
import top.lyunz.fangkeji.service.fuserService;
import top.lyunz.fangkeji.tools.SimpleResult;
import top.lyunz.fangkeji.tools.img;

@Controller
@RequestMapping("/app")
public class appController {
	@Resource
	private AttendanceRecordService AttendanceRecordService;

	@Resource
	private equipmentService equipmentService;

	/**
	 * 心跳
	 * 
	 * @param sN
	 * @return
	 */
	@RequestMapping("/getbysn/{sn}")
	public @ResponseBody SimpleResult getbysn(@PathVariable("sn") String sN) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		Equipment equipmentBySn = equipmentService.getEquipmentBySn(sN);
		equipmentService.updEquipmentByesN(new Equipment(null, null, null, null, df.format(new Date()) + "", sN, null,
				1, null, null, null, null, null));
		if (equipmentBySn.geteDoorStatus() != null && equipmentBySn.geteDoorStatus() != 0
				|| equipmentBySn.geteLivingBodyStatus() != null && equipmentBySn.geteLivingBodyStatus() != 0
				|| equipmentBySn.geteUserSynchronization() != null && equipmentBySn.geteUserSynchronization() != 0
				|| equipmentBySn.getAttendanceConfigSynchronization() != null
						&& equipmentBySn.getAttendanceConfigSynchronization() != 0
						&& equipmentBySn.getAttendanceConfigSynchronization() != null) {
			equipmentService.updEquipmentByesN(new Equipment(null, null, null, null, null, sN, null, 1, 0, 0, 0, 0, 0));
		}
		return new SimpleResult(equipmentBySn);
	}

	/**
	 * 首次用户同步
	 */
	@Resource
	private fuserService fuserService;

	@RequestMapping("/firstusersynchronization/{sn}")
	public @ResponseBody SimpleResult usersynchronization(@PathVariable("sn") String sN) {
		List<Fuser> getlikeeidList = null;
		Equipment equipmentBySn = equipmentService.getEquipmentBySn(sN);
		if (equipmentBySn != null) {
			getlikeeidList = fuserService.getFusersAllByCid(equipmentBySn.getcId());
		}
		// List<Fuser> getlikeeidList = fuserService.getlikeeidList(sN);
		return new SimpleResult(getlikeeidList);
	}

	/**
	 * 考勤设置同步
	 */
	@Resource
	private AttendanceConfigService AttendanceConfigService;

	@RequestMapping("/getattendanceConfig/{cid}")
	public @ResponseBody SimpleResult getattendanceConfig(@PathVariable("cid") int cId) {
		AttendanceConfig attendanceConfigByCid = AttendanceConfigService.getAttendanceConfigByCid(cId);
		return new SimpleResult(attendanceConfigByCid);
	}

	/**
	 * 添加通行记录
	 */
	@Resource
	private TravelRecordsService TravelRecordsService;

	@RequestMapping("/addTravelRecords")
	public @ResponseBody SimpleResult addTravelRecords(TravelRecordsadd travelRecordsadd) throws ParseException {

		String tTime = travelRecordsadd.gettTime();
		Integer uId = travelRecordsadd.getuId();
		String eSn = travelRecordsadd.geteSn();
		Integer cId = travelRecordsadd.getcId();
		String Img = null;
		MultipartFile file = travelRecordsadd.getStrangerImg();
		if (file != null) {

			String originalFilename = file.getOriginalFilename();
			originalFilename += ".jpg";
			String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
			img img = new img();
			String timeString = System.currentTimeMillis() + "";
			Img = timeString + prefix;
			img.uploadingImg(file, Img);
		}

		int is = TravelRecordsService.addTravelRecords(new TravelRecords(null, tTime, uId, cId, Img, eSn));
		AttendanceConfig attendanceConfigByCid = AttendanceConfigService.getAttendanceConfigByCid(cId);
		if (attendanceConfigByCid != null) {
			String attendanceTime = attendanceConfigByCid.getAttendanceTime();
			String closingTime = attendanceConfigByCid.getClosingTime();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String daString = df.format(date);
			attendanceTime = daString + " " + attendanceTime + ":00";
			closingTime = daString + " " + closingTime + ":00";
			Date attendanceTimedate = sdf.parse(attendanceTime);
			Date closingTimedate = sdf.parse(closingTime);
			Date nowdate = sdf.parse(tTime);
			List<AttendanceRecord> allAttendanceRecordByUidandday = AttendanceRecordService
					.getAllAttendanceRecordByUidandday(new selAttendanceRecord(uId, df.format(nowdate)));
			if (allAttendanceRecordByUidandday.size() == 0) {
				if (nowdate.before(attendanceTimedate)) {
					int addAttendanceRecord = AttendanceRecordService
							.addAttendanceRecord(new AttendanceRecord(null, tTime, uId, 1, eSn));

				} else if (nowdate.after(attendanceTimedate)) {
					int addAttendanceRecord = AttendanceRecordService
							.addAttendanceRecord(new AttendanceRecord(null, tTime, uId, 3, eSn));
				}
			} else if (allAttendanceRecordByUidandday.size() == 1) {
				if (nowdate.before(closingTimedate)) {
					int addAttendanceRecord = AttendanceRecordService
							.addAttendanceRecord(new AttendanceRecord(null, tTime, uId, 4, eSn));
				} else if (nowdate.after(closingTimedate)) {
					int addAttendanceRecord = AttendanceRecordService
							.addAttendanceRecord(new AttendanceRecord(null, tTime, uId, 2, eSn));
				}
			} else if (allAttendanceRecordByUidandday.size() == 2) {
				AttendanceRecord max = allAttendanceRecordByUidandday.get(0);
				if (sdf.parse(max.getArTime()).after(sdf.parse(allAttendanceRecordByUidandday.get(1).getArTime()))) {
					max = allAttendanceRecordByUidandday.get(1);
				}
				max.setArTime(tTime);
				int updAttendanceRecord = AttendanceRecordService.updAttendanceRecord(max);
			}
		}
		if (is > 0) {
			return new SimpleResult(new TravelRecords(null, tTime, uId, cId, Img, eSn));
		} else {
			return new SimpleResult(false);
		}
	}

	/**
	 * 添加打卡记录
	 */

	@Resource
	private appointmentMemberService appointmentMemberService;
	@Resource
	private AppointmentRecordService AppointmentRecordService;

	/**
	 * 上传拜访记录
	 * 
	 * @param apId
	 * @return
	 */
	@RequestMapping("/completedAppointment/{apId}")
	public @ResponseBody SimpleResult completedAppointment(@PathVariable int apId) {
		AppointmentRecord appointmentRecord = new AppointmentRecord();
		appointmentRecord.setApId(apId);
		appointmentRecord.setApStatus(3);
		int is = AppointmentRecordService.updAppointmentRecord(appointmentRecord);
		return new SimpleResult(is > 0 ? true : false);
	}

	@RequestMapping("/test")
	public @ResponseBody SimpleResult test() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<AttendanceRecord> allAttendanceRecordByUidandday = AttendanceRecordService
				.getAllAttendanceRecordByUidandday(new selAttendanceRecord(1, "2019"));
		for (AttendanceRecord attendanceRecord : allAttendanceRecordByUidandday) {
			Date parse = sdf.parse(attendanceRecord.getArTime());
			String str = sdf.format(parse);
			System.out.println(str);
		}
		return new SimpleResult(allAttendanceRecordByUidandday);

	}

	@RequestMapping("app")
	public String app() {
		return "page/app/index";
	}
}
